package com.chain.qs.modules.biz.web;

import com.chain.qs.common.persistence.Page;
import com.chain.qs.common.utils.DateUtils;
import com.chain.qs.common.utils.excel.ExportExcel;
import com.chain.qs.modules.biz.entity.*;
import com.chain.qs.modules.biz.listener.ConsumerMessageListener;
import com.chain.qs.modules.biz.service.*;
import com.chain.qs.modules.sys.entity.Office;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(value = "${adminPath}/biz/wRecord")
public class WarningRecordController extends SeniorController {

    @Resource
    private StationService stationService;
    @Resource
    private WarningRecordService warningRecordService;
    @Resource
    private ConsumerMessageListener consumerMessageListener;

    @ModelAttribute("warningRecord")
    public WarningRecord get(String sgroupId, String itemId, String paramKid, String isSuspend, String operator, String timeBegin, String timeEnd) {
        return warningRecordService.getWarningRecord(sgroupId, itemId, paramKid, isSuspend, operator, timeBegin, timeEnd);
    }

    @RequestMapping(value = "")
    public String sgroup(Model model) {
        model.addAttribute("list", stationService.findListWithOffice(new Office()));
        return "modules/biz/warningRecord";
    }


    @RequestMapping(value = "warningRecords")
    public String warningRecords(WarningRecord warningRecord, HttpServletRequest request, HttpServletResponse response) {//查询提交功能
        Page<WarningRecord> page = warningRecordService.findPage(new Page<WarningRecord>(request, response), warningRecord);
        List<String> titleList = new ExportExcel().titleList(WarningRecord.class, 1);
        page.setTitleList(titleList);
        new ExportExcel(WarningRecord.class).setDataList(page.getList(), 1);
        page.setDataList(ExportExcel.dataList);
        return renderString(response, page);
    }


    @RequestMapping(value = "exportRecords")
    public void exportRecords(WarningRecord warningRecord, String officeId, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try { // 生成文件的名称
            StringBuffer fileName = warningRecordService.fileTitle(warningRecord, officeId);
            String fileTitle = fileName.toString();
            fileName.append(DateUtils.getDate("yyyyMMddHHmmss")).append(".xlsx");
            new ExportExcel(fileTitle, WarningRecord.class).setDataList(warningRecordService.findList(warningRecord)).write(response, fileName.toString()).dispose();
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出失败！失败信息：" + e.getMessage());
        }
    }

    @RequestMapping(value = "test")
    public void test() {
        consumerMessageListener.onMessage(null);
    }
}
